//
//  WFTabbar.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/9.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit

class WFTabbar: UITabBar {

    //定义闭包点击按钮进行传值
    var composeBtnClosure:(()->())?
    
    // MARK:  - 懒加载最中间的那个btn
    private lazy var composeBtn:UIButton = {
        let btn = UIButton()
        //添加点击事件
        btn .addTarget(self, action: "clickOnComposeBtn", forControlEvents: .TouchUpInside)
        //设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        //设置背景色
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        //设置图片自适应
        btn.sizeToFit()
        return btn
    }()
    
    // MARK:  - 点击中间的发送按钮就执行
    func clickOnComposeBtn(){
        print("中间按钮被点击")
        //如果闭包不为空,我就执行闭包里的代码
        composeBtnClosure?()
    }
    
    // MARK:  - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeBtn)
    }
    
    //防止xib创建后冲突,就会提示如下的警报
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK:  -  对tabbar里面的控件重新布局
    override func layoutSubviews() {
        //访问父类的方法,如果不写,将很有可能报错
        super.layoutSubviews()
        
        //对于btn而言,布局是在正中间的
        composeBtn.centerX = width * 0.5
        composeBtn.centerY = height * 0.5
        
        
        //重新写其余控件的布局
        let itemWidth = width / 5
        
        var index = 0
        //对子控件进行遍历
        for value in subviews {
            //如果是UITabBarButton的话
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
            value.width = itemWidth
            value.x = CGFloat(index) * itemWidth
                //给中间的按钮移出位置(对第二个tabbarbutton设置后,将第三个向后移)
//                if index == 1 {
//                    index++
//                }
                index++
                
                if index == 2 {
                    index++
                }

                
                
            }
        }
        
        
        print("WFTabbar")
//        for (UIControl tabBarButton in self.subviews) {
//            if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
//                [tabBarButton addTarget:selfaction:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//            }
//        }
        for tabBarButton in subviews{
            if tabBarButton .isKindOfClass(NSClassFromString("UITabBarButton")!){
                let tabBarbuttonControl = tabBarButton as! UIControl
                tabBarbuttonControl .addTarget(self, action: "tabBarButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            }
        }
    }
    
    
//    - (void)tabBarButtonClick:(UIControl *)tabBarButton
//    {
//    for (UIView *imageView in tabBarButton.subviews) {
//    if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
//    //需要实现的帧动画,这里根据需求自定义
//    CAKeyframeAnimation *animation = [CAKeyframeAnimationanimation];
//    animation.keyPath = @"transform.scale";
//    animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
//    animation.duration = 1;
//    animation.calculationMode = kCAAnimationCubic;
//    //把动画添加上去就OK了
//    [imageView.layeraddAnimation:animation forKey:nil];
//    }
//    }
//    }
    func tabBarButtonClick(tabBarButton:UIControl){
        for imgView in tabBarButton.subviews{
            if imgView .isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!){
                let animation = CAKeyframeAnimation()
                animation.keyPath = "transform.scale"
                animation.values = [1.0,1.3,0.9,1.15,0.95,1.02,1.0]
                animation.duration = 1
                animation.calculationMode = kCAAnimationCubic
                imgView.layer.addAnimation(animation, forKey: nil)
            }
        }
    }

}
